#include <stdio.h>

#include "lapackUtil.h"
#include "comp_llpmatrix.h"
#include "lpmatrix.h"
#include "pcdwrapper.h"
//#include "lapackUtil.h"
#include <string>
#include <fstream>
#include <iostream>

#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/foreach.hpp>
/*
extern "C" {
#include "cs.h"
}
*/
extern "C" void dgetrf_(long int &m, long int &n,
        double *a, long int &lda,
        long int* ipiv, long int &info);
extern "C" void dspev_(char &jobz, char &uplo,
        long int &n, double *ap,
        double *w, double *z,
        long int &ldz, double *work, long int &info);



//#include "arlnsmat.h"
//#include "arlsnsym.h"

int main(int argc, char* argv[]) {

    double v[] = {0.5, 0, 0, 2, 1, 0, 3, 2, 1}, *d = new double[3];

    solve_eigen(v, d, 3);


    int piv;
    for (int i = 0; i < 3; ++i) {
        printf("lambda %2.20f\n", d[i]);
        for (int j = 0; j < 3; ++j) {
            piv = i * 3 + j;
            printf("%2.20f ", v[piv]);
        }
        printf("\n");
    }

    delete[] d;


    if (argc < 7) {
        printf("not enough parameters: %d\n", argc);
        return 0;
    }
    std::string s;

    pcdwrapper pw;

    s.clear();
    s.append(argv[1]);

    printf("file name: %s\n", s.c_str());

    pw.readMFile(s.c_str());
    printf("%d vertices read.\n", pw.m_points.size());

    /*
        if (pw.readMatFile(argv[2])) {
            printf("%d function value read.\n", pw.m_val.size());
        } else {
            printf("Read Mat file failed\n.");
            return 0;
        }
     */

    s.clear();
    s.append(argv[2]);

    printf("%s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);

    std::vector<unsigned int> I, J;
    std::vector<double> S, B;
    //cal_pcd_laplace(pw.m_points, I, J, S);
    //printf("pcd done for %s\n", argv[2]);
    //save_IJS(s.c_str(), I, J, S) ? printf("PCD IJS true\n") : printf("PCD IJS false\n");

    I.clear();
    J.clear();
    S.clear();

    s.clear();
    s.append(argv[3]);


    cal_sym_pcd_laplace(pw.m_points, I, J, S, B);
    printf("%d %d %d %d %s\n", I.size(), J.size(), S.size(), B.size(), s.c_str());
    //    save_IJS("pcdlaplace_vd_matrix.txt", I, J, S) ? printf("IJSB true\n") : printf("IJSB false\n");
    save_IJS(s.c_str(), I, J, S) ? printf("save sym IJS true\n") : printf("save sym IJS false\n");
    //    writeDoubleArray(B, "pcdlaplace_vd_mass.txt");
    writeDoubleArray(B, argv[4]);

    //    check_connectivity_symmetricity_cs(m);

    int n = B.size();
    printf("B.size %d\n", n);
    double *p = new double[n * n];
    d = new double[n];


    
    convert_IJS_2_col_sq_matrix(p, I, J, S, n);
    solve_eigen(p, d, n);

    writeDoubleRowSqMatrix(p, n, argv[5]);
    writeDoubleRow(d, n, argv[6]);

    
    delete[] p;
    delete[] d;

    return 0;
}
